package com.gyc.datastructure.queue;

/**
 * @author gyc
 * @date 2017/12/28
 * 链表实现队列
 */
public class MyQueue<E> {
    private Node<E> head=null;
    private Node<E> tail=null;

    public boolean isEnpty(){
        return head==tail;
    }

    public void put(E value){
        Node<E> newnode=new Node<E>(value);
        if(head==null && tail==null){
            head=tail=newnode;
        }else {
            tail.next=newnode;
            tail=newnode;
        }
    }

    public E pop(){
        if(this.isEnpty()){
            return null;
        }
        E value=head.value;
        head=head.next;
        return value;
    }

    public int size(){
        int len=0;
        Node<E> temp=head;
        while(temp!=null){
            len++;
            temp=temp.next;
        }
        return len;
    }

    public static void main(String[] args){
        MyQueue<Integer> myQueue=new MyQueue<>();
        myQueue.put(1);
        myQueue.put(2);
        myQueue.put(3);
        myQueue.put(4);
        System.out.println("长度：="+myQueue.size());
        System.out.println("队列首元素为："+myQueue.pop());
        System.out.println("队列首元素为："+myQueue.pop());
        System.out.println("队列首元素为："+myQueue.pop());
    }

}
